namespace cnVery.Cms
{
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;

    public class details
    {
        public static int Add(detail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into details(");
            builder.Append("ordernumber,productid,count");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.ordernumber + "',");
            builder.Append("" + model.productid + ",");
            builder.Append("" + model.count + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            string sql = "SELECT @@IDENTITY AS Expr";
            object scalar = connection.GetScalar(sql);
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return int.Parse(scalar.ToString());
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete details ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from details where id=" + id + "");
            builder.Append("select count(1) from votes where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int) scalar;
            return (num > 0);
        }

        public static DataTable GetDetail(string ordernumber)
        {
            StringBuilder builder = new StringBuilder();
            string str = "select p.id,p.name,p.code,p.price,d.count,p.discount,(p.price*d.count*p.discount/100) as total from products p,details d where ordernumber='" + ordernumber + "' and productid=p.id";
            builder.Append(str);
            builder.Append(" order by p.id");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from details ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id ");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static detail GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from details ");
            builder.Append(" where id=" + id);
            detail detail = new detail();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            detail.id = id;
            if (dataTable.Rows.Count > 0)
            {
                detail.ordernumber = dataTable.Rows[0]["ordernumber"].ToString().Trim();
                if (dataTable.Rows[0]["productid"].ToString() != "")
                {
                    detail.productid = int.Parse(dataTable.Rows[0]["productid"].ToString());
                }
                if (dataTable.Rows[0]["count"].ToString() != "")
                {
                    detail.count = int.Parse(dataTable.Rows[0]["count"].ToString());
                }
                return detail;
            }
            return null;
        }

        public static void Update(detail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update details set ");
            builder.Append("ordernumber='" + model.ordernumber + "',");
            builder.Append("productid=" + model.productid + ",");
            builder.Append("count=" + model.count + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

